package gui.server.listener;


import gui.GuiCommon;
import gui.MenuName;
import gui.common.DefaultGroupList;
import gui.common.LoginDialogue;
import gui.common.msg.MsgType;
import gui.common.msg.SysMessage;
import gui.server.GroupAdminList;
import gui.server.Server;
import gui.server.UserListPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;


import data.Group;
import data.User;

public class ServerMenuEventListenr implements ActionListener{

	
	
	public void actionPerformed(ActionEvent e) {
		Server svr=Server.getInstance();
		String menuName = e.getActionCommand();
		if (menuName.equals(MenuName.INFO)) {
			SysMessage msg=new SysMessage();
			msg.subject="About Discuss Forum";
			msg.detail="Create by Scm && YM"+ "\n2009-05-18";
			GuiCommon.showAlertWindow(msg);
        }else if(menuName.equals(MenuName.LOGIN)){
        	if(svr.isServerStart()){
	        	LoginDialogue ld=new LoginDialogue(svr);
	        	ld.setVisible(true);
        	}
        }else if(menuName.equals(MenuName.LOGOUT)){
        	if(svr.isServerStart()){
        		SysMessage msg=null;
        		try {
        			msg =showGroupList(false);
        			msg.type=MsgType.Success;
        			
				} catch (Exception e2) {
					e2.printStackTrace();
					msg.detail=e2.getMessage();
					msg.type=MsgType.Error;
				}
        		
        		if(msg.type!=MsgType.Success){
            		GuiCommon.showAlertWindow(msg);
            	}
        		
        	}
        }else if(menuName.equals(MenuName.CONNECT)){
        	SysMessage msg=null;
        	if(svr.isServerStart()){
        		msg=svr.connectServer();
        	if(msg.type==MsgType.Success){
        		try {
        			showGroupList(false);
				} catch (Exception e2) {
					msg.detail=msg.detail+"\n"+e2.getMessage();
				}
        	}
        	}else{
        		msg=new SysMessage();
        		msg.detail="You must start Server first";
        	}
        	if(msg.type!=MsgType.Success){
        		GuiCommon.showAlertWindow(msg);
        	}
        	
        }else if(menuName.equals(MenuName.START)){
        	
        	GuiCommon.showAlertWindow(svr.startServer());
        }else if(menuName.equals(MenuName.STOP)){
        	
        	GuiCommon.showAlertWindow(svr.stopServer());
        }else if(menuName.equals(MenuName.GUP_MGR)){
        	SysMessage msg=new SysMessage();
        	if(svr.isServerStart()){
        		try {
        			showGroupList(true);
        			msg.type=MsgType.Success;
					
				} catch (Exception e2) {
					msg.type=MsgType.Error;
					msg.detail=e2.getMessage();
					e2.printStackTrace();
				}
        	}
        	if(msg.type!=MsgType.Success){
        		GuiCommon.showAlertWindow(msg);
        	}
        }else if(menuName.equals(MenuName.USR_MGR)){
        	SysMessage msg=new SysMessage();
        	if(svr.isServerStart()){
        		try {
        			showUserList();
        			msg.type=MsgType.Success;
					
				} catch (Exception e2) {
					msg.type=MsgType.Error;
					msg.detail=e2.getMessage();
					e2.printStackTrace();
				}
        	}
        	if(msg.type!=MsgType.Success){
        		GuiCommon.showAlertWindow(msg);
        	}
        }
	}

	public static SysMessage showGroupList(boolean isAdmin) throws Exception{
		
		Server svr=Server.getInstance();
		SysMessage	msg=new SysMessage();
		
		List<Group> groupInfo=null;
		DefaultGroupList gp=null;
		groupInfo=svr.getDataClient().getRemoteServer().getGroupList(isAdmin);
		String welcomeInfo=svr.getDataClient().getRemoteServer().getWelcomeInfo();
		if(isAdmin){
			gp=new GroupAdminList(groupInfo,new ServerButtonListener(null),welcomeInfo);
		}else{
			gp=new DefaultGroupList(groupInfo,new ServerButtonListener(null),welcomeInfo);
			gp.showButtonBeforeLogin();
		}
		svr.setContent(gp);
		msg.type=MsgType.Success;
		
		return msg;
	
	}
	
	public static SysMessage showUserList() throws Exception{
		Server svr=Server.getInstance();
		SysMessage	msg=new SysMessage();
		
		List<User> userInfo=null;
		UserListPanel up=null;
		userInfo=svr.getDataClient().getRemoteServer().getUserList();
		up=new UserListPanel(userInfo,new ServerButtonListener(null));
		svr.setContent(up);
		msg.type=MsgType.Success;
		
		return msg;
	}
	
	
}
